package ru.spbu.math.android.cbrf.dataprovider.cbrf;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import ru.spbu.math.android.cbrf.R;
import ru.spbu.math.android.cbrf.model.Category;
import ru.spbu.math.android.cbrf.model.RateType;
import ru.spbu.math.android.cbrf.model.StockItem;
import android.content.res.Resources;

/**
 * Class for parsing data about all available RateTypes for CBRF and put it in the List of RateType.
 * This class extends DefaultHandler
 * @author  Svetlana Zemlyanskaya
 */

//<RateType>
//	<category>Currency</category>
//	<code>GBP</code>
//	<name>British Pound Sterling</name>
//	<nominalValue>1</nominalValue>
//	<nominalName></nominalName>
//</RateType>

public class RateTypeSaxHandler extends DefaultHandler {

	private static String currencyCategory;
	private static String metalCategory;
	
	private final String RATE_TYPE = "RateType";
    private final String CATEGORY = "category";
	private final String CODE = "code";
    private final String NAME = "name";
    private final String NOMINALVALUE = "nominalValue";
    private final String NOMINALNAME = "nominalName";
    
    private Resources resources;
    
    private List<RateType> currencyList;
    private List<RateType> metalList;
    
    private Category category;
    private RateType rateType;
    private StockItem source;
    
    private StringBuilder builder;

    
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        builder.append(ch, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
    	if (localName.equalsIgnoreCase(CATEGORY)) {
    		this.category = new Category(builder.toString().trim());
    	}
    	else if(localName.equalsIgnoreCase(CODE)){
    		source.setCode(builder.toString().trim());
    	}
    	else if(localName.equalsIgnoreCase(NAME)){
    		source.setName(builder.toString().trim());
    	}
    	else if(localName.equalsIgnoreCase(NOMINALVALUE)){
    		String str = builder.toString().trim();
    		source.setNominalValue(Integer.parseInt(str));
    	}
    	else if(localName.equalsIgnoreCase(NOMINALNAME)){
    		source.setNominalName(builder.toString().trim());
    	}
    	else if(localName.equalsIgnoreCase(RATE_TYPE)){
			StockItem target = new StockItem("RUB", "Rubble", 1, "");
			rateType = new RateType(source, target, convertToId(source, target));
    		if(category.getName().equals(currencyCategory)){
    			currencyList.add(rateType);
    		}
    		else if(category.getName().equals(metalCategory)){
    			metalList.add(rateType);
    		}
    	}	
    	builder.setLength(0);
    }

    @Override
    public void startDocument() throws SAXException {
		currencyCategory = resources.getString(R.string.Cbrf_Currency);
		metalCategory = resources.getString(R.string.Cbrf_Metal);
        currencyList = new ArrayList<RateType>();
        metalList = new ArrayList<RateType>();
        builder = new StringBuilder();
    }

    @Override
    public void startElement(String uri, String localName, String name,
                             Attributes attributes) throws SAXException {
        if (localName.equalsIgnoreCase(RATE_TYPE)) {
        	this.source = new StockItem();
        }
    }

    /**
	 * @return currency list from XML
	 */
    public List<RateType> getCurrency() {
        return currencyList;
    }

    /**
	 * @return currency list from XML
	 */
    public List<RateType> getMetal() {
        return metalList;
    }
    
    /**
     * Return id for the RateType
     * @param source
     * @param target
     * @return id
     */
	private String convertToId(StockItem source, StockItem target) {
		return source.getCode() + "/" + target.getCode();
	}
	
	/**
	 * Set resources
	 * @param res
	 */
	public void setResources(Resources res){
		resources = res;
	}
    
}